package com.ln.jvm;

/**
 * <pre>
 *  逃逸分析实例
 * </pre>
 *
 * @Author zhangj
 * @Date 2022/7/1 15:53
 */
public class EscapeAnalysisClass {

    public static B b;

    static class B {
        public void printClassName(G g) {
            System.out.println(g.getClass().getName());
        }
    }

    static class G {
        // 给全局变量赋值，发生逃逸
        public void globalVariablePointEscape() {
            b = new B();
        }

        // 方法的返回值，发生逃逸
        public B methodPointEscape() {
            return new B();
        }

        // 实例引用发生逃逸
        public void instancePassPointEscape() {
            methodPointEscape().printClassName(this);
        }
    }

}
